package main
import (
	"fmt"
	"os"
	"bufio"
	"io"
)
//关于文件的拷贝Copy
//io/func Copy(dst Writer, src Reader) (written int64, err error)
//将src的数据拷贝到dst，直到在src上到达EOF或发生错误。返回拷贝的字节数和遇到的第一个错误。
//将一个文件Copy到另一个文件！
func main(){
	filepath1 := "D:/Myimages/user.jpg"
	filepath2 := "D:/uploadfiles/temp.jpg"
	//调用自己写的函数完成文件的拷贝：
	num,err := CopyFile(filepath2,filepath1)
	fmt.Println("拷贝完成：",num)
	if err != nil {
		fmt.Println("文件拷贝失败！Error : ",err)		
	}
}

//接收两个文件路径 srcFileName,dstFileName
func CopyFile(dstFileName string,srcFileName string)(written int64,err error){
	srcfile,err1 := os.Open(srcFileName)
	if err1 != nil{
		fmt.Println("文件打开失败！Error : ",err1)
		return 
	}
	//通过srcFileName获得Reader
	reader := bufio.NewReader(srcfile)

	dstfile,err2 := os.OpenFile(dstFileName,os.O_WRONLY | os.O_CREATE,0666)
	if err2 != nil{
		fmt.Println("文件打开失败！Error : ",err2)
		return
	}
	//通过dstFileName获得Writer
	writer := bufio.NewWriter(dstfile)

	//开始进行拷贝
	written,err = io.Copy(writer,reader)

	defer srcfile.Close()
	defer dstfile.Close()
	return 
}